package tom.change.swing.login;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import tom.change.swing.index.Index;
import tom.change.util.ConvertColor;

public class Login extends JFrame implements ActionListener, MouseListener,
        FocusListener, KeyListener
{

    /**
     * 序列化
     */
    private static final long serialVersionUID = 1L;
    private GridBagLayout gridBagLayout;
    private GridBagConstraints gridBagConstraints;
    private JLabel userNameLabel;
    private JTextField userNameField;
    private JCheckBox userNameBox;
    private JLabel passLabel;
    private JPasswordField passField;
    private JCheckBox passBox;
    private JButton loginBtn;
    private JPanel panel;

    public Login()
    {
        initComponent();
        initJframe();
        initEvent();
        test();
    }

    private void initJframe()
    {
        setTitle("人脸识别系统登录");
        // 设置窗口显示的位置及大小
        int my_width = 600;
        int my_heigth = 400;

        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();

        setBounds(((int) screensize.getWidth() - my_width) / 2,
                ((int) screensize.getHeight() - my_heigth) / 2, my_width,
                my_heigth);

        setDefaultCloseOperation(3);

        setVisible(true);
    }

    private void initComponent()
    {
        this.panel = new JPanel();
        setContentPane(this.panel);

        this.gridBagLayout = new GridBagLayout();

        this.gridBagConstraints = new GridBagConstraints();

        this.gridBagConstraints.fill = 1;
        this.userNameLabel = new JLabel("用户名");
        makeComponent(this.userNameLabel, 1, 1);
        this.userNameField = new JTextField();
        this.userNameField.setColumns(12);
        makeComponent(this.userNameField, 1, 1);
        this.userNameBox = new JCheckBox("记住用户名", false);
        makeComponent(this.userNameBox, 0, 1);
        makeLine(3);

        this.passLabel = new JLabel("密码");
        makeComponent(this.passLabel, 1, 1);
        this.passField = new JPasswordField();
        makeComponent(this.passField, 1, 1);
        this.passBox = new JCheckBox("记住密码", false);
        makeComponent(this.passBox, 0, 1);
        makeLine(3);

        this.loginBtn = new JButton("登录");

        this.loginBtn.setBackground(ConvertColor.getColorFromHex("1C86EE"));

        this.loginBtn.setFont(new Font("楷体", 1, 16));
        this.loginBtn.setForeground(ConvertColor.getColorFromHex("141414"));
        makeComponent(this.loginBtn, 3, 1);

        this.panel.setLayout(this.gridBagLayout);
    }

    private void initEvent()
    {
        this.loginBtn.addMouseListener(this);
        this.userNameField.addFocusListener(this);

        this.userNameField.addKeyListener(this);
        this.userNameBox.addKeyListener(this);
        this.passField.addKeyListener(this);
        this.passBox.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyPressed(KeyEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void focusGained(FocusEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void focusLost(FocusEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent e)
    {
        // 鼠标点击事件
        if (e.getSource() == this.loginBtn)
        {
            startLogin();
        }
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        // TODO Auto-generated method stub

    }

    /********************************* 辅助方法 *********************************/

    public void makeLine(int time)
    {
        for (int i = 0; i < time; i++)
            makeComponent(null, 0, 1);
    }

    public void makeComponent(Component component, int width, int height)
    {
        if (component == null)
        {
            component = new JLabel(" ");
        }

        this.gridBagConstraints.gridwidth = width;
        this.gridBagConstraints.gridheight = height;
        this.gridBagLayout.addLayoutComponent(component,
                this.gridBagConstraints);
        this.panel.add(component);
    }

    public void makeComponent(Component component, int width, int height,
            double weightx, double weighty)
    {
        this.gridBagConstraints.weightx = weightx;
        this.gridBagConstraints.weighty = weighty;
        this.gridBagConstraints.gridwidth = width;
        this.gridBagConstraints.gridheight = height;
        this.panel.add(component);
    }

    // 进行登录验证
    private void startLogin()
    {
        String userName = this.userNameField.getText();
        String passWord = this.passField.getText();
        if ((userName == null) || ("".equalsIgnoreCase(userName)))
        {
            JOptionPane.showMessageDialog(this.userNameField, "用户名不能为空", "用户名",
                    0);
            return;
        }
        if ((passWord == null) || ("".equalsIgnoreCase(passWord)))
        {
            JOptionPane.showMessageDialog(this.passField, "密码不能为空", "密码", 0);
            return;
        }
        dispose();
        new Index();
    }
    //暂时加入死数据
    private void test()
    {
      this.userNameField.setText("test@qq.com");
      this.passField.setText("test");
    }
}